/* 
THIS DOFILE PRODUCES TABLES AND FIGURES RELATING TO A REGRESSION DISCONTINUITY
DESIGN IN

"Regulation by Reputation? Intermediaries, Labor Abuses, and International Migration" 
- A. Nilesh Fernando (University of Notre Dame); corresponding author (nilesh.fernando@nd.edu)  
- Niharika Singh (University of Notre Dame) 

These includes Table 4, Appendix C13, and Appendix C14

See readme.docx for details 

*/ 

clear
set matsize 1000
set more off 
#delimit;

cd "~/Desktop/migrec_replication/do/";

use  ../dta_secure/4_firm_mn.dta, clear;

gen yr_13 = (year == 2013); 
gen yr_14 = (year == 2014); 
gen yr_15 = (year == 2015); 

* expand data to permit centering of thresholds;

expand 2 if performance_rate != . & elig_firm == 1 , gen(fake_data) ;

* create pooled treatment variable; 

gen pooled_treat = 0 if elig_firm == 1; 

replace pooled_treat = 1 if performance_rate > 19.9 & performance_rate < 35 & performance_rate != .  ; 
replace pooled_treat = 1 if performance_rate > 34.9 & performance_rate < 50 & performance_rate != . & fake_data == 1; 
replace pooled_treat = 1 if performance_rate > 49.9 & performance_rate < 60 & performance_rate != . & fake_data == 0; 
replace pooled_treat = 1 if performance_rate > 59.9 & performance_rate < 80 & performance_rate != . & fake_data == 1; 


gen cutoffz_1 = (performance_rate < 35 & performance_rate != . & fake_data == 0) & elig_firm == 1; 
gen cutoffz_2 = (performance_rate > 19.9 & performance_rate < 50 & performance_rate != . & fake_data == 1)  & elig_firm == 1; 
gen cutoffz_3 = (performance_rate > 34.9 & performance_rate < 60 & performance_rate != . & fake_data == 0)  & elig_firm == 1; 
gen cutoffz_4 = (performance_rate > 49.9 & performance_rate < 80 & performance_rate != . & fake_data == 1)  & elig_firm == 1; 


** center cutoffs around zero for single rdrobust; 

gen cent_perf = .; 

replace cent_perf = performance_rate - 20 if performance_rate <= 19.9  & performance_rate != . & fake_data == 0 ; 
replace cent_perf = performance_rate - 20 if performance_rate >= 0 & performance_rate < 35 & performance_rate != . & fake_data == 0 ; 
replace cent_perf = performance_rate - 35 if performance_rate >= 20 & performance_rate < 50 & performance_rate != . &  fake_data == 1; 
replace cent_perf = performance_rate - 50 if performance_rate >= 35 & performance_rate < 60 & performance_rate != . & fake_data == 0 ; 
replace cent_perf = performance_rate - 60 if performance_rate >= 50 & performance_rate < 80 & performance_rate != . & fake_data == 1; 


gen cent_treat = (cent_perf >= 0); 

gen cent_treat_perf = cent_treat*cent_perf; 


*----------------------------------------------------*
* TABLE 4 - THE EFFECT OF RECEIVING A HIGHER RATING  * 
*----------------------------------------------------*;

		eststo clear;
	
  		eststo, title("Job Orders"): rdrobust num_job_orders cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(1) vce(cluster agency_id)  kernel(uni)  all ; 
	    
	    sum num_job_orders if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
	    

		eststo, title("Migrants"): rdrobust migrant cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(1) vce(cluster agency_id) kernel(uni) all ; 
	    
	    sum mig_t995 if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
		
		
		eststo, title("Domestic Share"): rdrobust domestic_worker_share cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(1) vce(cluster agency_id) kernel(uni)  all ; 
	    
	    sum domestic_share if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
		
		eststo, title("Pre-Complaints"): rdrobust pre_complainants cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0) p(1) vce(cluster agency_id) kernel(uni)   all ; 
	    
	    sum pre_complainants if elig_firm == 1  & postm == 1;
		estadd scalar ymean = r(mean);

	
 	estadd scalar ar=e(r2_a);
	
	esttab using "../out/table4.tex", b(%12.3f) se(%12.3f) 
	starlevels(* 0.10 ** 0.05 *** 0.01) booktabs replace label
	stats(N ymean, label("N" "Depvar. Mean") fmt("%12.0f" "%12.2f")) 
	coeflabel(cent_perf "Additional Star" ) mtitles nonotes ;
		
*--------------------------------------------*
* APPENDIX C13 - RD PLOTS FOR JOB ORDERS	 *
*--------------------------------------------*;

preserve; 

keep if cent_perf >= -3.498 & cent_perf <= 3.498; 

rdplot num_job_orders cent_perf if elig_firm == 1 & postm == 1 , c(0) 
vce(cluster agency_id) p(1)  kernel(uni) covs(yr_13 yr_15)
 graph_options(graphregion(fcolor(white) lcolor(white)) xline(0,lcolor(white)) xline(0,lcolor(gs10) lp(dash))
 title("CUT", color(white)) ytitle("# Job Orders")
  xtitle("Combined Score") legend(off) ylabel(,nogrid));
  
graph export ../out/c13_a.pdf, replace;


rdplot num_job_orders cent_perf if elig_firm == 1 & postm == 1 , c(0) 
vce(cluster agency_id) p(2)  kernel(uni) covs(yr_13 yr_15)
 graph_options(graphregion(fcolor(white) lcolor(white)) xline(0,lcolor(white)) xline(0,lcolor(gs10) lp(dash))
 title("CUT", color(white)) ytitle("# Job Orders")
  xtitle("Combined Score") legend(off) ylabel(,nogrid));
  
graph export ../out/c13_b.pdf, replace;


rdplot num_job_orders cent_perf if elig_firm == 1 & postm == 1 , c(0) 
vce(cluster agency_id) p(3)  kernel(uni) covs(yr_13 yr_15)
  graph_options(graphregion(fcolor(white) lcolor(white)) xline(0,lcolor(white)) xline(0,lcolor(gs10) lp(dash))
 title("CUT", color(white)) ytitle("# Job Orders")
  xtitle("Combined Score") legend(off) ylabel(,nogrid));
  
graph export ../out/c13_c.pdf, replace;


restore;

*---------------------------------------------*
* APPENDIX C14 - ROBUSTNESS OF RD ESTIMATES;  *
*---------------------------------------------*;

** Panel A: First-Order polynomial uses the same output as Table 4;  

**  Panel B: Second-Order Polynomial;

		eststo clear;
	

  	  	eststo, title("Job Orders"): rdrobust num_job_orders cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(2) vce(cluster agency_id)  kernel(uni) covs(yr_13 yr_15 ) all ; 
	    
	    sum num_job_orders if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
	    

		eststo, title("Migrants"): rdrobust migrant cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(2) vce(cluster agency_id) kernel(uni) covs(yr_13 yr_14 yr_15 ) all ; 
	    
	    sum mig_t995 if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
		
		
		eststo, title("Domestic Share"): rdrobust domestic_worker_share cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(2) vce(cluster agency_id) kernel(uni) covs(yr_13 yr_14 yr_15 ) all ; 
	    
	    sum domestic_share if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
		
		eststo, title("Pre-Complaints"): rdrobust pre_complainants cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0) p(2) vce(cluster agency_id) kernel(uni)  covs(yr_13 yr_14 yr_15 ) all ; 
	    
	    sum pre_complainants if elig_firm == 1  & postm == 1;
		estadd scalar ymean = r(mean);
     
	
 	estadd scalar ar=e(r2_a);

esttab using "../out/c14_a.tex", b(%12.3f) se(%12.3f) 
		starlevels(* 0.10 ** 0.05 *** 0.01) booktabs replace label

		stats(N ymean, label("N" "Depvar. Mean") fmt("%12.0f" "%12.2f")) 
		coeflabel(cent_perf "Treatment" ) mtitles
		nonotes ;


** Panel C: Third-Order Polynomial; 

		eststo clear;
	
  	  	eststo, title("Job Orders"): rdrobust num_job_orders cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(3) vce(cluster agency_id)  kernel(uni) covs(yr_13 yr_15 ) all ; 
	    
	    sum num_job_orders if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
	    

		eststo, title("Migrants"): rdrobust migrant cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(3) vce(cluster agency_id) kernel(uni) covs(yr_13 yr_14 yr_15 ) all ; 
	    
	    sum mig_t995 if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
		
		eststo, title("Domestic Share"): rdrobust domestic_worker_share cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0)  p(3) vce(cluster agency_id) kernel(uni) covs(yr_13 yr_14 yr_15 ) all ; 
	    
	    sum domestic_share if elig_firm == 1 & postm == 1 ;
		estadd scalar ymean = r(mean);
		
		eststo, title("Pre-Complaints"): rdrobust pre_complainants cent_perf if 
	    elig_firm == 1 & postm == 1 , c(0) p(3) vce(cluster agency_id) kernel(uni)  covs(yr_13 yr_14 yr_15 ) all ; 
	    
	    sum pre_complainants if elig_firm == 1  & postm == 1;
		estadd scalar ymean = r(mean);

estadd scalar ar=e(r2_a);

esttab using "../out/c14_b.tex", b(%12.3f) se(%12.3f) 
		starlevels(* 0.10 ** 0.05 *** 0.01) booktabs replace label

		stats(N ymean, label("N" "Depvar. Mean") fmt("%12.0f" "%12.2f")) 
		coeflabel(cent_perf "Treatment" ) mtitles
		nonotes ;

